//
//  KaihuViewController.m
//  PlayerWorld
//
//  Created by 汪的一声就哭了！ on 16/8/15.
//  Copyright © 2016年 刘成. All rights reserved.
//

#import "KaihuViewController.h"
#import "LabTFView.h"
#import "AFNetworking.h"
@interface KaihuViewController ()
@property (nonatomic, strong) LabTFView *firstLT;
@property (nonatomic, strong) LabTFView *secLT;
@property (nonatomic, strong) LabTFView *thirdLT;
@property (nonatomic, strong) LabTFView *fourthLT;
@property (nonatomic, strong) LabTFView *fifthLT;
@property (nonatomic, strong) UIButton *btn;
@end

@implementation KaihuViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"开户中心";
    self.view.backgroundColor = [UIColor whiteColor];
    self.firstLT = [[LabTFView alloc] initWithFrame:CGRectMake(0, 100, self.view.bounds.size.width, 28)];
    [self.view addSubview:self.firstLT];
    self.firstLT.label.text = @"登陆账号名:";
    self.firstLT.textField.placeholder = @"只能输入字母加数字6-10位，取消中文名注册";
    
    self.secLT = [[LabTFView alloc] initWithFrame:CGRectMake(0, 160, self.view.bounds.size.width, 28)];
    [self.view addSubview:self.secLT];
    self.secLT.label.text = @"用户昵称:";
    self.secLT.textField.placeholder = @"（可选）请输入用户昵称";
    
    self.thirdLT = [[LabTFView alloc] initWithFrame:CGRectMake(0, 220, self.view.bounds.size.width, 28)];
    [self.view addSubview:self.thirdLT];
    self.thirdLT.label.text = @"返点设置:";
    self.thirdLT.textField.placeholder = @"范围 0 ~ 12.8";
    
    self.fourthLT = [[LabTFView alloc] initWithFrame:CGRectMake(0, 280, self.view.bounds.size.width, 28)];
    [self.view addSubview:self.fourthLT];
    self.fourthLT.label.text = @"登录密码:";
    self.fourthLT.textField.placeholder = @"由数字和字母组成6-16个字符;且必须包含数字和字母";
    
    self.fifthLT = [[LabTFView alloc] initWithFrame:CGRectMake(0, 340, self.view.bounds.size.width, 28)];
    [self.view addSubview:self.fifthLT];
    self.fifthLT.label.text = @"确认密码:";
    self.fifthLT.textField.placeholder = @"包含数字和字母6-10位,不能有连续三位相同的字符";
    
    self.btn = [[UIButton alloc] initWithFrame:CGRectMake(20, 400, self.view.bounds.size.width - 40, 40)];
    self.btn.backgroundColor = [UIColor colorWithRed:36/255.0f green:11/255.0f blue:54/255.0f alpha:1];
    [self.view addSubview:self.btn];
    [self.btn setTitle:@"添加账户" forState:UIControlStateNormal];
    [self.btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    [self.view addGestureRecognizer:tap];
}
- (void)tapAction:(UITapGestureRecognizer *)tap
{
    [self.fifthLT.textField resignFirstResponder];
    [self.firstLT.textField resignFirstResponder];
    [self.secLT.textField resignFirstResponder];
    [self.thirdLT.textField resignFirstResponder];
    [self.fourthLT.textField resignFirstResponder];
}
- (void)btnAction:(UIButton *)btn
{
    if ([self.fourthLT.textField.text isEqualToString:self.fifthLT.textField.text]) {
        [self obtainData];
    }else{
        UIAlertController *alerView = [UIAlertController alertControllerWithTitle:@"提示信息" message:@"两次密码输入不同" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        [alerView addAction:action];
        [self presentViewController:alerView animated:YES completion:nil];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)obtainData
{
    // 启动系统风火轮
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    
    //服务器给的域名
    NSString *domainStr = @"http://666.lefun6.com/";
    
    //创建一个可变字典
    NSMutableDictionary *parametersDic = [NSMutableDictionary dictionary];
    //往字典里面添加需要提交的参数
    [parametersDic setObject:@"User" forKey:@"Model"];
    [parametersDic setObject:@"MyTeam_Add" forKey:@"Action"];
    [parametersDic setObject:@"1" forKey:@"U_Type"];
    [parametersDic setObject:self.firstLT.textField.text forKey:@"U_UserName"];
    [parametersDic setObject:self.secLT.textField.text forKey:@"U_Nickname"];
    [parametersDic setObject:self.thirdLT.textField.text forKey:@"U_RebateA"];
    [parametersDic setObject:self.fourthLT.textField.text forKey:@"U_UserPass"];
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    //以post的形式提交，POST的参数就是上面的域名，parameters的参数是一个字典类型，将上面的字典作为它的参数
    [manager POST:domainStr parameters:parametersDic progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        // 解析成功隐藏系统风火轮
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        //        NSLog(@"123%@",[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil]);
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
        NSLog(@"qqqqqqq%@",dic);
        if ([dic objectForKey:@"msg"]) {
            UIAlertController *alerView = [UIAlertController alertControllerWithTitle:@"提示信息" message:[dic objectForKey:@"msg"] preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *action = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                
            }];
            [alerView addAction:action];
            [self presentViewController:alerView animated:YES completion:nil];
        }
        else{
            UIAlertController *alerView = [UIAlertController alertControllerWithTitle:@"提示信息" message:@"开户成功" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *action = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                [self.navigationController popViewControllerAnimated:YES];
            }];
            [alerView addAction:action];
            [self presentViewController:alerView animated:YES completion:nil];
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        // 解析失败隐藏系统风火轮(可以打印error.userInfo查看错误信息)
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        
    }];
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
